package com.ruiyi.json.parser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ruiyi.baseparser.AbstractParser;
import com.ruiyi.baseparser.Group;
import com.ruiyi.baseparser.GroupParser;
import com.ruiyi.baseparser.ParserUtil;
import com.ruiyi.json.Json_Job;
import com.ruiyi.json.Json_JobModel;


public class JobModelParser extends AbstractParser<Json_JobModel> {
//	private static final String TAG = "JobModelParser\t";

	@SuppressWarnings("unchecked")
	@Override
	public Json_JobModel parse(JSONObject json) throws JSONException {

		if (RuiyiUtil.isResultPackage(json)) {
			return null;
		}

		Json_JobModel obj = new Json_JobModel();

		if (json.has("companyid"))
			obj.setCompanyID(json.getLong("companyid"));

		if (json.has("companyname"))
			obj.setCompanyName(json.getString("companyname"));

		if (json.has("joblist")) {
			JSONArray array = json.getJSONArray("joblist");
			String content = array.toString();
			try {
				Group<Json_Job> joblist = (Group<Json_Job>) ParserUtil.consume(
						new GroupParser(new JobParser()), content);
				obj.setJobList(joblist);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return obj;
	}
}

class JobParser extends AbstractParser<Json_Job> {
//	private static final String TAG = "JobParser:\t";
	String jobname; // job名称
	String city; // job所在地区
	long number; // job个数
	String workyear; // job工作经验年限
	String education; // job所需最低学历
	String jobdesc; // job描述
	String cutoffdate; // job招聘截止日期
	String salary; // job薪资待遇

	@Override
	public Json_Job parse(JSONObject json) throws JSONException {
		if (RuiyiUtil.isResultPackage(json)) {
			return null;
		}
		Json_Job obj = new Json_Job();
		if (json.has("jobname")) {
			obj.setJobName(json.getString("jobname"));
		}
		if (json.has("city")) {
			obj.setCity(json.getString("city"));
		}
		if (json.has("workyear")) {
			obj.setWorkYear(json.getString("workyear"));
		}
		if (json.has("education")) {
			obj.setEducation(json.getString("education"));
		}
		if (json.has("jobdesc")) {
			obj.setJobDescription(json.getString("jobdesc"));
		}
		if (json.has("cutoffdate")) {
			obj.setCutoffDate(json.getString("cutoffdate"));
		}
		if (json.has("salary")) {
			obj.setSalary(json.getString("salary"));
		}
		if (json.has("number")) {
			obj.setJobNumbers(json.getLong("number"));
		}
		return obj;
	}
}